//
//  transactionTableVC.m
//  Recycling
//
//  Created by jzd on 2018/5/18.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "transactionTableVC.h"
#import "TransacitionTableCell.h"
#import "transactionModel.h"

@interface transactionTableVC ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (assign,nonatomic)int page;
@property (strong,nonatomic)NSMutableArray *dataArray;
@end

@implementation transactionTableVC

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"交易记录";
    self.view.backgroundColor = BGCOLOR;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TransacitionTableCell class]) bundle:nil] forCellReuseIdentifier:@"TransacitionTableCell"];
    self.tableView.mj_header = [SJRefresh headerWithRefreshingTarget:self refreshingAction:@selector(requestNewData)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [SJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[SJNetWorkTool sharedTool].tasks makeObjectsPerformSelector:@selector(cancel)];
}
-(void)requestMoreData{
    self.page ++;
    NSDictionary *params = @{
                             @"pageno":@(self.page),
                             @"limit" : @(pageNum),
                             @"uid":@([UserInfoTool shareUserInfoTool].Info.ID)
                             };
    [[SJNetWorkTool sharedTool]requestWithURLString:getAccountDetail parameters:params method:GET callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            NSString *msg = [responseObject objectForKey:@"ErrorMessage"];
            id data = [responseObject objectForKey:@"Data"];
            if (code == 1) {
                if (![data isKindOfClass:[NSNull class]]) {
                    
                    int total = [[data objectForKey:@"total"] intValue];
                    if (total<pageNum) {
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                    NSArray *arr = [transactionModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"rows"]];
                    if (arr.count != 0) {
                        [self.dataArray addObjectsFromArray:arr];
                    }else{
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        [self.navigationController.view showToast:@"没有更多数据" completion:nil];
                        return ;
                    }
                    
                }else{
                    [self.navigationController.view showToast:@"暂无数据" completion:nil];
                }
            }else{
                [self.navigationController.view showToast:msg completion:nil];
            }
        }
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    }];
}
-(void)requestNewData{
    self.page = 1;
    NSDictionary *params = @{
                             @"pageno":@(self.page),
                             @"limit" : @(pageNum),
                             @"uid":@([UserInfoTool shareUserInfoTool].Info.ID)
                             };
    [[SJNetWorkTool sharedTool]requestWithURLString:getAccountDetail parameters:params method:GET callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            NSString *msg = [responseObject objectForKey:@"ErrorMessage"];
            id data = [responseObject objectForKey:@"Data"];
            if (code == 1) {
                if (![data isKindOfClass:[NSNull class]]) {
                    
                    int total = [[data objectForKey:@"total"] intValue];
                    if (total<pageNum) {
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                    NSArray *arr = [transactionModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"rows"]];
                    if (arr.count != 0) {
                         self.dataArray = arr.mutableCopy;
                    }else{
                        [self.tableView.mj_header endRefreshing];
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        [self.navigationController.view showToast:@"暂无数据" completion:nil];
                        return ;
                    }
                    
                }else{
                    [self.navigationController.view showToast:@"暂无数据" completion:nil];
                }
            }else{
                [self.navigationController.view showToast:msg completion:nil];
            }
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
    }];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    transactionModel *model = [self.dataArray objectAtIndex:indexPath.row];
    TransacitionTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TransacitionTableCell"];
    [cell setModel:model];
    return cell;
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    return [UIImage imageNamed:@"empty"];
    
}
- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView {
    NSLog(@"空白页点击事件");
    //重新加载默认的数据
    [self.tableView.mj_header beginRefreshing];
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    
    NSString *text = @"没有相关数据";
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14.0f], NSForegroundColorAttributeName: TITLECOLOR};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
    
}
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    
    NSString *text = @"重新加载";
    
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new]; paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    
    paragraph.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName: TITLECOLOR, NSParagraphStyleAttributeName: paragraph}; return [[NSAttributedString alloc] initWithString:text attributes:attributes];
    
}

@end
